import axios, { AxiosRequestConfig } from 'axios'
//import { API_URL, LOGIN_TOKEN_KEY } from '../config'

export interface Options {
    method : 'POST' | 'GET' | 'DELETE' | 'PUT';
    data? : any;
    params? : object;
}

export interface Result {
    success : boolean;
    code? : number;
    data? : any;
    msg : string;
    [key:string] : any;
}

class Http {
    private sending:boolean = false;

    async post<B = any>(url: string, data?: any, params? : object){
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.post<B>(url, data, params)
        this.sending = false
        return result
    }

    static post<B = any>(url: string, data?: any, params? : object){
        let options:Options = {
            method : 'POST'
        }
        if(data) {
            options.data = data
            // if(data instanceof FormData) {
            //     options.processData = false
            // }
        }

        if(params) {
            options.params = params
        }

        return Http.send<B>(url, options)
    }

    async get<B = any>(url: string, params? : object, data?: any){
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.get<B>(url, params, data)
        this.sending = false
        return result
    }

    static get<B = any>(url: string, params? : object, data?: any){
        let options:Options = {
            method : 'GET'
        }

        if(params) {
            options.params = params
        }

        if(data) {
            options.data = data
        }

        return Http.send<B>(url, options)
    }

    async put<B = any>(url: string, data?: any, params? : object){
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.put<B>(url, data, params)
        this.sending = false
        return result
    }

    static put<B = any>(url: string, data?: any, params? : object){
        let options:Options = {
            method : 'PUT'
        }
        if(data) {
            options.data = data
            // if(data instanceof FormData) {
            //     options.processData = false
            // }
        }

        if(params) {
            options.params = params
        }

        return Http.send<B>(url, options)
    }

    async delete<B = any>(url: string, data?: any, params? : object){
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.delete<B>(url, data, params)
        this.sending = false
        return result
    }

    static delete<B = any>(url: string, data?: any, params? : object){
        let options:Options = {
            method : 'DELETE'
        }

        if(params) {
            options.params = params
        }

        if(data) {
            options.data = data
        }

        return Http.send<B>(url, options)
    }

    static async send<B>(url: string, options: Options) : Promise<ApiResult<B>>{
        try {

            let config:AxiosRequestConfig = {
                url : url,
                ...options,
                responseType : 'json',
            }
            

            let response = await axios(config)

            let result:ApiResult<B> = {
                success : true,
                msg : '',
                data : null,
                code : 0
            }


            if(response.data) {
                const {success, msg, data, code, ...other} = response.data
                result.success = success
                result.msg = msg
                result.data = data
                result.code = code

                for(let k in other) {
                    result[k] = other[k]
                }
            }

            return result
           
        } catch (error:any) {
            return {
                success : false,
                msg : error.response.data ? error.response.data.message : error.message,
                data : null
            } as ApiResult
        }
    }
}

export default Http